package com.liang.blog.reception.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.liang.blog.client.user.UserFeignClient;
import com.liang.blog.common.exception.BlogException;
import com.liang.blog.common.result.Result;
import com.liang.blog.common.result.ResultCodeEnum;
import com.liang.blog.model.BlogSessionList;
import com.liang.blog.reception.mapper.BlogSessionListMapper;
import com.liang.blog.reception.service.BlogArticleService;
import com.liang.blog.reception.service.BlogSessionListService;
import com.liang.blog.security.utils.UUIDUtil;
import com.liang.blog.vo.user.UserInfoVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2024-05-02-9:52
 */
@Service
public class BlogSessionListServiceImpl implements BlogSessionListService {

    @Autowired
    private BlogSessionListMapper blogSessionListMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public BlogSessionList getById(String curSessionListId) {
        return blogSessionListMapper.getById(curSessionListId);
    }

    @Override
    public BlogSessionList getSessionListByUser(Long blogUserId, Long toBlogUserId) {
        return blogSessionListMapper.getSessionListByUser(blogUserId, toBlogUserId);
    }

    @Override
    public BlogSessionList createReverseSessionList(BlogSessionList sessionList, boolean isAddUnReadCount) {
        Result<UserInfoVo> userResult = userFeignClient.getUserById(sessionList.getBlogUserId());

        if (ResultCodeEnum.SUCCESS.getCode().intValue() != userResult.getCode().intValue()){
            throw new BlogException(ResultCodeEnum.SERVICE_ERROR);
        }

        UserInfoVo user = userResult.getData();

        BlogSessionList tmpSessionList = new BlogSessionList();

        tmpSessionList.setSessionId(UUIDUtil.getUUID());
        tmpSessionList.setBlogUserId(sessionList.getToBlogUserId());
        tmpSessionList.setToBlogUserId(sessionList.getBlogUserId());
        tmpSessionList.setBlogListName(user.getNickName());
        tmpSessionList.setToBlogUserAvatar(user.getAvatar());
        if(isAddUnReadCount){
            tmpSessionList.setBlogUnReadCount(1);
        }else{
            tmpSessionList.setBlogUnReadCount(0);
        }

        blogSessionListMapper.insert(tmpSessionList);
        return tmpSessionList;
    }

    /**
     * 当发送消息时，接受消息的用户不在当前聊天窗口时，增加未读消息数
     * @param sessionId
     */
    @Override
    public void addUnReadCount(String sessionId) {
        BlogSessionList sessionList = blogSessionListMapper.selectById(sessionId);
        sessionList.setBlogUnReadCount(sessionList.getBlogUnReadCount() + 1);
        try {
            this.blogSessionListMapper.updateById(sessionList);
        }catch (Exception e){
            e.printStackTrace();
            throw new BlogException("消息发送失败！");
        }
    }

    @Override
    public List<BlogSessionList> getSessionListHistory(Long toBlogUserId) {
        return blogSessionListMapper.getSessionListHistory(toBlogUserId);
    }

    /**
     * 创建会话对象
     * @param fromUserId    发起会话的用户id
     * @param toUserId  目标用户的id
     * @return
     */
    @Override
    public BlogSessionList createSessionList(Long fromUserId, Long toUserId) {
        //判断会话对象是否已经存在
        BlogSessionList sessionListTemp = this.getSessionListByUser(fromUserId, toUserId);
        if(ObjectUtil.isNotEmpty(sessionListTemp)){
            return sessionListTemp;
        }
        Result<UserInfoVo> userResult = userFeignClient.getUserById(toUserId);

        if (ResultCodeEnum.SUCCESS.getCode().intValue() != userResult.getCode().intValue()){
            throw new BlogException(ResultCodeEnum.SERVICE_ERROR);
        }

        UserInfoVo user = userResult.getData();

        if(ObjectUtil.isEmpty(user)){
            throw new BlogException("聊天对象不存在");
        }

        BlogSessionList sessionList = new BlogSessionList();
        sessionList.setSessionId(UUIDUtil.getUUID());
        sessionList.setBlogUserId(fromUserId);
        sessionList.setToBlogUserId(toUserId);
        sessionList.setToBlogUserAvatar(user.getAvatar());
        sessionList.setBlogUnReadCount(0);
        sessionList.setBlogListName(user.getNickName());

        blogSessionListMapper.insert(sessionList);

        return sessionList;
    }

    @Override
    public void removeById(String sessionListId) {
        blogSessionListMapper.removeById(sessionListId);
    }

    /**
     * 将sessionList的未读消息数置零
     * @param sessionList
     */
    @Override
    public void emptySessionListUnReadCount(BlogSessionList sessionList) {
        sessionList.setBlogUnReadCount(0);
        blogSessionListMapper.updateById(sessionList);
    }

    /**
     * 修改当前会话的备注
     * @param sessionListId
     * @param remark
     */
    @Override
    public void editSessionListRemark(String sessionListId, String remark) {
        BlogSessionList sessionList = blogSessionListMapper.selectById(sessionListId);
        sessionList.setBlogRemark(remark);
        blogSessionListMapper.updateById(sessionList);
    }
}
